package com.shux.inventory.biz.impl;

import java.util.concurrent.ArrayBlockingQueue;

import com.shux.inventory.biz.IRequestAsynProcessBiz;
import com.shux.inventory.request.Request;
import com.shux.inventory.request.RequestQueue;
/**
 * 更加产品ID路由到对应的队列
 * 将请求加入到队列中
 * @author 734621
 *
 */
public class RequestAsynProcessBizImpl implements IRequestAsynProcessBiz {
	@Override
	public void process(Request request) {
		
		ArrayBlockingQueue<Request> queue = getRouteQueue(request.getProductId());
		queue.add(request);
	}
	
	private ArrayBlockingQueue<Request> getRouteQueue(Integer productId){
		RequestQueue requestQueue = RequestQueue.getInstance();
		String key = String.valueOf(productId);
		int h;
		int hash = (key == null) ? 0 : (h = key.hashCode()) & (h >>> 16);
		int index  = (requestQueue.getQueueSize() - 1) & hash;
		return requestQueue.getQueueByIndex(index);
	}

}
